package com.zxl.always.on.display.model;

import com.zxl.always.on.display.constant.LockerSetting;

import java.util.Calendar;

/**
 * Created by Administrator on 2018/3/21.
 */

public class TimerHelper {

    static final String[][] Arrays = {
            {"07:03", "06:01", "08:05"},
            {"07:03", "07:01", "07:05"},
            {"07:03", "06:01", "07:05"},
            {"07:10", "06:01", "07:05"},
            {"05:03", "06:01", "08:05"},
            {"10:03", "06:01", "08:05"},

            {"04:03", "08:01", "06:05"},
            {"06:04", "08:01", "06:05"},
            {"10:03", "08:01", "06:05"},
            {"08:03", "08:01", "06:05"},
    };


    private static TimerBean getTimer(String data) {
        return new TimerBean(Integer.parseInt(data.substring(0, 2)), Integer.parseInt(data.substring(3, 5)));
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int minute = calendar.get(Calendar.MINUTE);
        String s = calendar.get(Calendar.HOUR_OF_DAY) + ":" + (minute < 10 ? "0" + minute : minute);
        System.out.println(s);
        for (String[] array : Arrays) {
            System.out.println(isInDate(s, array[1], array[2]));
        }
    }

    public static boolean isInDate() {
        if (LockerSetting.sShowTimerSetting) {
            return true;
        } else {
            Calendar calendar = Calendar.getInstance();
            int minute = calendar.get(Calendar.MINUTE);
            String currentData = calendar.get(Calendar.HOUR_OF_DAY) + ":" + (minute < 10 ? "0" + minute : minute);
            String startData = LockerSetting.sStartHour + ":" + LockerSetting.sStartMin;
            String endData = LockerSetting.sEndHour + ":" + LockerSetting.sEndMin;
            return isInDate(currentData, startData, endData);
        }
    }

    private static boolean isInDate(String currentData, String startData, String endData) {
        TimerBean startTimerBean = getTimer(startData);
        TimerBean endTimerBean = getTimer(endData);

        TimerBean currentTimerBean = getTimer(currentData);
        if (startTimerBean.hour <= endTimerBean.hour) {
            return isInDate(currentTimerBean, startTimerBean, endTimerBean);
        } else {
            return !isInDate(currentTimerBean, endTimerBean, startTimerBean);
        }
    }

    private static boolean isInDate(TimerBean currentTimerBean, TimerBean startTimerBean, TimerBean endTimerBean) {
        if (startTimerBean.hour > endTimerBean.hour) {
            throw new RuntimeException("isInDate");
        }
        if (currentTimerBean.hour > startTimerBean.hour && currentTimerBean.hour < endTimerBean.hour) {
            return true;
        } else if (currentTimerBean.hour == startTimerBean.hour) {
            if (endTimerBean.hour == currentTimerBean.hour) {
                if (startTimerBean.min < currentTimerBean.min && endTimerBean.min > currentTimerBean.min) {
                    return true;
                }
            } else {
                if (startTimerBean.min < currentTimerBean.min) {
                    return true;
                }
            }
        } else if (endTimerBean.hour == currentTimerBean.hour) {
            if (endTimerBean.min > currentTimerBean.min) {
                return true;
            }
        }
        return false;
    }


    private static class TimerBean {

        public int hour;

        public int min;

        public TimerBean(int hour, int min) {
            this.hour = hour;
            this.min = min;
        }

        @Override
        public String toString() {
            return hour + " : " + min;
        }
    }

}


